package cn.iscorpio;

import java.util.concurrent.*;

public class Singleton {

    private Singleton() {
    }

    private static Singleton INSTANCE;

    public static Singleton getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        Singleton singleton = exec.submit(new MyThread()).get();
        boolean flag = true;
        while (flag) {
            Singleton singleton1 = exec.submit(new MyThread()).get();
            if(!singleton.equals(singleton1)) {
                flag =false;
                System.out.println(singleton);
                System.out.println(singleton1);
            }
        }

    }

    public static class MyThread implements Callable<Singleton> {
        @Override
        public Singleton call() {
            return Singleton.getInstance();
        }
    }
}
